package com.leetcode.matrix;

import java.util.HashSet;
import java.util.Set;

public class RobotSim {
    public static void main(String[] args) {
        System.out.println(-1%4);
    }

    public int robotSim(int[] commands, int[][] obstacles) {
        int[][] dirs = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        int px = 0;
        int py = 0;
        int d = 1;
        int maxDis = 0;
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < obstacles.length; i++) {
            set.add(obstacles[i][0] * 60001 + obstacles[i][1]);
        }
        for (int i = 0; i < commands.length; i++) {
            if (commands[i] < 0) {
                d += commands[i] == -1 ? 1:-1;
                d %= 4;
                if (d < 0) {
                    d += 4;
                }
            } else {
                for (int j = 0; j < commands[i]; j++) {
                    if (set.contains((px + dirs[d][0]) * 60001 + py + dirs[d][1])) {
                        break;
                    }
                    px += dirs[d][0];
                    py += dirs[d][1];
                    maxDis = Math.max(maxDis, px * px + py * py);
                }
            }
        }
        return maxDis;
    }
}
